﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prototype123.data_access;
using prototype123.controllers;
using prototype123.models;

namespace prototype123
{
    public partial class login_form : Form
    {
        public login_form()
        {
            InitializeComponent();
        }

        // If the user clicks the btnLogin, then the System checks if the Username
        // exists, and then checks if the password is correct.
        // A main_form will open after this occurs, and this form will go invisible
        private void btnLogin_Click(object sender, EventArgs e)
        {
            
            // Ensure that txtPassword and txtUsername are not empty. If they are empty, then do nothing
            if (txtPassword.Text == "" && txtUsername.Text == "") {
                return;
            }

            LoginController lg_ctrl = new LoginController(); // Create object to check if the username exists 

            bool success = lg_ctrl.Login(txtUsername.Text, txtPassword.Text); // bool login_success = lg_ctrl.login();

            if (success != true) // if login_succes is fale, then give error message
            {
                MessageBox.Show("Invalid Username or Password. Please try again.");
                return;
            }

            else if (success == true) // If login_success is true, then exit this screen, and open the main_screen.
            {
                this.Visible = false;
                prototype123.Program.m_frm = new main_form();
                prototype123.Program.m_frm.Show();
                prototype123.Program.m_frm.Visible = true;
            }
   
        }

        // When the user clicks the cancel button, the program closes
        private void btnCancel_Click(object sender, EventArgs e)
        {
            // close the program
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
}
